﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace 人事工资管理系统
{
    public partial class Formlogin : Form
    {
        public Formlogin()
        {
            InitializeComponent();
        }
        PMSClass.DBOperate operate = new PMSClass.DBOperate();
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                MessageBox.Show("请输入用户名", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            else
            {
                if (textBox2.Text == "")
                {
                    MessageBox.Show("请输入密码", "警告", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
                else
                {
                    string name = textBox1.Text.Trim();
                    string pwd = textBox2.Text.Trim();
                    SqlConnection conn = PMSClass.DBConnection.MyConnection();
                    conn.Open();
                    SqlCommand cmd = new SqlCommand("select * from tb_User where UserName='" + name + "'and UserPwd='" + pwd + "'", conn);
                    SqlDataReader sdr = cmd.ExecuteReader();//得到数据读取器对象
                    sdr.Read();//读取一条记录
                    if (sdr.HasRows)//判断是否包含数据
                    {
                        string time = DateTime.Now.ToString();//得到系统时间字符串
                        string sql = "update tb_User set LoginTime='" //设置更新数据库的SQL语句
                                    + time + "' where UserName='" + name + "'";
                        operate.OperateData(sql);//更新数据库内容
                        conn.Close();//关闭数据库连接
                        this.Hide();//隐藏窗体
                        FormMain Main = new FormMain();//创建主窗体对象
                        Main.User = name;//为主窗体字段赋值
                        Main.Logintime = time;//为主窗体字段赋值
                        Main.Show();//显示主窗体
                    }
                    else
                    {
                        textBox1.Text = "";//清空用户名
                        textBox2.Text = "";//清空密码
                        MessageBox.Show("用户名或密码错误！", "提示",//弹出消息对话框
                            MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }


                }
            } 
        }

        private void button2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void textBox2_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == 13)
            {
                button1_Click(sender, e);
            }
        }

        
    }
}
